<?php
require_once ROOT_PATH . '/lib/models/surveys/Answers.php';//Chinh Nguyen add
require_once ROOT_PATH . '/lib/models/surveys/Options.php';//Chinh Nguyen add
require_once ROOT_PATH . '/lib/models/surveys/Questions.php';//Chinh Nguyen add
require_once ROOT_PATH . '/lib/models/surveys/Surveys.php';//Chinh Nguyen add  
require_once ROOT_PATH . '/FirePHPCore/FirePHP.class.php';
require_once ROOT_PATH . '/lib/models/hrfunct/EmpInfo.php';
include ROOT_PATH.'/plugins/FusionChartsFree/Code/PHPClass/Includes/FusionCharts_Gen.php';

$answerByUsers = Answers::getAnswersBySurvey($records->getId());
$customQuestions = Questions::getCustomQuestionsBySurvey($records->getId());
if(count($customQuestions)>0){
	$customtext = $customQuestions[0]->getText();
}else{
	$customtext ="";
}

?>

<h2><?php echo $records->getSurveyName()." summary:".Answers::getTotalAnswersBySurvey($records->getId())." result(s)"; ?><hr/></h2>
<form id="frmCancelLeave" name="frmCancelLeave" method="post" action="">
<table border="0" cellpadding="0" cellspacing="0">
  <thead>
  	<tr>
		<th class="tableTopLeft"></th>
		<th class="tableTopMiddle" width="150"></th>
		<th class="tableTopMiddle" ></th>
		<th class="tableTopMiddle"></th>
    	<th class="tableTopMiddle" ></th>
    	<?php if ($customtext != ""){ ?>
    	<th class="tableTopMiddle" ></th>
    	<?php } ?>
		<th class="tableTopRight"></th>
	</tr>
	<tr>
		<th class="tableMiddleLeft"></th>
		<th class="tableMiddleMiddle">Employee name</th>
    	<th  class="tableMiddleMiddle">Score</th>
    	<?php if ($customtext != ""){ ?>
    	<th  class="tableMiddleMiddle"><?php echo $customtext?> </th>
    	<?php } ?>
    	<th class="tableMiddleMiddle">Date submit</th>
    	<th  class="tableMiddleMiddle"></th>    	
    	
		<th class="tableMiddleRight"></th>
	</tr>
  </thead>
  <tbody>
<?php
	$i = 0;	
	$j =0;
	$minScore = count(Questions::getQuestionsBySurvey($records->getId()));	
	$maxScore =0;
	$minScoreName="";
	$maxScoreName= array();	
 	if(count($answerByUsers) >0){ ?>
 		
 		<?php foreach($answerByUsers as $answerByUser){
 			
 			$scores = Answers::getAnswersScoreBySurveyUser($records->getId(),$answerByUser->getUser()) ;
 			$answers = Answers::getCustomAnswersBySurveyUser($records->getId(),NULL,$answerByUser->getUser()) ;
 			
 			$customAnswer = "";
 			if(count($answers)>0){ 				
 				foreach ($answers as $item){
 					$question = Questions::getQuestionById($item->getQuestionID());
 					if($question->getAnswerType()==4){ 	
 											
 						$customAnswer = $item->getValue();
 					}
 				}	
 				
 			}
 			if($maxScore = $scores['personalscore']){
 				$j++; 				
 				$maxScoreName[$i]=EmpInfo::getFullName($answerByUser->getUser());
 			}
 			if($maxScore < $scores['personalscore']){
 				$maxScore = $scores['personalscore'];
 				$j =0;
 				$maxScoreName= array();	
 				$maxScoreName[$j]=EmpInfo::getFullName($answerByUser->getUser());
 			}
 			if($minScore > $scores['personalscore']){
 				$minScore = $scores['personalscore'];
 				$minScoreName=EmpInfo::getFullName($answerByUser->getUser());
 			}
 				
		 	if(!($i%2)) {$cssClass = 'odd';
		 	} else {  	$cssClass = 'even'; }
		 	$i++;			 	
	 	?>
			 <tr>
			  	<td class="tableMiddleLeft"></td>
			  	<td><a href="CentralController.php?surveycode=Surveys&action=ShowPersonalResults&surveyid=<?php echo $records->getId(); ?>&userid=<?php echo $answerByUser->getUser(); ?>" target="rightMenu"> <?php echo EmpInfo::getFullName($answerByUser->getUser()); ?> </a></td>
			  	<td> <?php echo $scores['personalscore']."/".$scores['totalscore'] ?> </td>
			  	<?php if ($customtext != ""){ ?>
			  	<td> <?php echo $customAnswer ?> </td>
			  	<?php } ?>
			    <td colspan ="2" ><?php echo $answerByUser->getDateSubmit(); ?></td>
				<td class="tableMiddleRight"></td>
			 </tr>	
 		<?php	
 			}
 			
 		}else { ?>
 			 <tr>
			  	<td class="tableMiddleLeft"></td>
			  	<td> </td>
			    <td colspan ="3" class="<?php echo $cssClass; ?>" style="color:blue;font-size:12px;"><?php echo "No Answer" ?></td>
				<td class="tableMiddleRight"></td>
			 </tr>	
 		<?php }
?>
  
  </tbody>
  <tfoot>
  	<tr>
		<td class="tableBottomLeft"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomMiddle"></td>
		<?php if ($customtext != ""){ ?>
		<td class="tableBottomMiddle"></td>
		<?php } ?>
		<td class="tableBottomRight"></td>
	</tr>
  </tfoot>

</table>
  <?php 
  if (count($answerByUsers)>0 && $maxScore>0){  ?>
  <table>
  	<tr>
		<td class=""></td>
		<td class=""><span style="font-size:13px;color:red;">Max score: <?php echo $maxScore."(". implode(",", $maxScoreName).")";?> </span></td>
		<td class=""></td>
		<td class=""></td>
		<td class=""></td>
		<td class=""></td>
	</tr>
	</table>
  <?php } ?>



